SurgeTrack = LibStub("AceAddon-3.0"):NewAddon("SurgeTrack", "AceEvent-3.0","AceConsole-3.0","AceTimer-3.0")
local ST = LibStub("AceAddon-3.0"):GetAddon("SurgeTrack", true)
local L = LibStub("AceLocale-3.0"):GetLocale(ST:GetName(), true)
local LSM = LibStub("LibSharedMedia-3.0", true)

function ST:OnInitialize()
 ST:LoadSTD()
 ST:LoadSTDB()
 ST:LoadSTF()
 ST:LoadSTLDB()
 ST:LoadSTC()
 ST:EnableModule("SurgeTrack_Aura")
 ST:EnableModule("SurgeTrack_Spell")

 ST:RegisterChatCommand("st", "ConsoleCmd")
 ST:RegisterChatCommand("surgetrack", "ConsoleCmd")

end

function ST:OnEnable()

 if not ST.db.profile.addon.enable then ST:Disable() return end

 ST:LDBUpdate(ST.IMG,ST.NAME)
 ST.root:Show()

 ST:RegisterEvent("COMBAT_LOG_EVENT")
 ST:RegisterEvent("UNIT_AURA")
 ST:RegisterEvent("COMBAT_TEXT_UPDATE")

end

function ST:OnDisable()

 ST:UnregisterEvent("COMBAT_LOG_EVENT")
 ST:UnregisterEvent("UNIT_AURA")
 ST:UnregisterEvent("COMBAT_TEXT_UPDATE")

 ST:DeleteContainer(1,ST.db.profile.unit.amount)
 if ST.root then ST.root:Hide() end
 ST:LDBUpdate(ST.IMG,"|cff666666"..ST.NAME.."|r")

end

function ST:Switch()
 if UnitAffectingCombat("player") then ST:Print(L["You are in combat."]) return end
 ST.db.profile.addon.enable = not ST.db.profile.addon.enable
 if ST.db.profile.addon.enable then ST:Enable() ST:Print(L["G_ENABLE"]) else ST:Disable() ST:Print(L["G_DISABLE"]) end
 LibStub("AceConfigRegistry-3.0"):NotifyChange(ST.NAME)
end

function ST:ConsoleCmd(cmd)
 local cmd = string.lower(cmd)
 if cmd == "g" then
  -- ST:GetModule("SurgeTrack_Aura", true):GatherMode(not ST.GATHER)
 elseif cmd == "enable" or cmd == "disable" or cmd == "on" or cmd == "off" then
  ST:Switch()
 elseif cmd == "lock" then
  ST:RootLock(true)
 elseif cmd == "unlock" then
  ST:RootLock(false)
 else
  LibStub("AceConfigDialog-3.0"):Open(ST.NAME)
 end
end

function ST:SoundNotification()
 PlaySoundFile(LSM:Fetch("sound", LSM:List("sound")[ST.db.profile.addon.sound]))
end

local function newContainerId(spellId)
 local i = 0
 while ST.queue[i].next do
  if not ST.container[ST.queue[i].next].spellId then
   ST.container[ST.queue[i].next].spellId = spellId
   return ST.queue[i].next
  end
  i = ST.queue[i].next
 end

 -- for i = 1 , ST.db.profile.unit.amount do
  -- if not ST.container[i].spellId then
   -- ST.container[i].spellId = spellId
   -- return i
  -- end
 -- end
 return false
end

function ST:updateItem(spellId,commit_info,mask)
 if floor(mask/tonumber("1",2))%2 == 1 then
  local containerId = newContainerId(spellId)
  if containerId then
   ST.active_item[spellId].containerId = containerId;
  else
   ST.wait[spellId] = true
  end
 end
 if floor(mask/tonumber("10",2))%2 == 1 then
  ST.active_item[spellId].spellName = commit_info.spellName or ST.active_item[spellId].spellName
  ST.active_item[spellId].spellIcon = commit_info.spellIcon or select(3,GetSpellInfo(spellId)) or ST.active_item[spellId].spellIcon
  ST.active_item[spellId].auraType = commit_info.auraType or ST.active_item[spellId].auraType
 end
 if floor(mask/tonumber("100",2))%2 == 1 then
  ST.active_item[spellId].event = commit_info.event or ST.active_item[spellId].event
 end
 if floor(mask/tonumber("1000",2))%2 == 1 then
  ST.active_item[spellId].subEvent = commit_info.subEvent or ST.active_item[spellId].subEvent
 end
 if floor(mask/tonumber("10000",2))%2 == 1 then
  ST.active_item[spellId].timestamp = commit_info.timestamp or ST.active_item[spellId].timestamp or GetTime()
  ST.active_item[spellId].duration = commit_info.duration or ST.active_item[spellId].duration or select(6, UnitBuff("player", ST.active_item[spellId].spellName)) or select(6 ,UnitDebuff("player", ST.active_item[spellId].spellName)) or 0
 end
 if floor(mask/tonumber("100000",2))%2 == 1 then
  ST.active_item[spellId].amount = commit_info.amount or ST.active_item[spellId].amount
 end

 local item = ST.active_item[spellId]
 if item.containerId then
  ST:LDBUpdate(item.spellIcon,item.spellName)
  if item.subEvent == "SPELL_AURA_APPLIED" or item.subEvent == "SPELL_ACTIVE" then
   ST:SoundNotification()
   ST:doFullScrFlash()
  end
  ST:updateContainer(item.containerId,mask)
 end
end

function ST:releaseItem(spellId)
 local item = ST.active_item[spellId]
 if item and item.containerId then
  ST:releaseContainer(item.containerId,true)
 end
 ST.active_item[spellId] = nil
 ST.wait[spellId] = nil
end
